/**
 * Copyright (c) 2011 - 2012,  Inc. All rights reserved.
 * @fileoverview   表单-inputText基础类(用于type=txt的input控件)
 * @author  bowen liu | liubowen082@163.com
 * @version 1.0 | 2013-04-13
 * @param
 * opt{
 * 	'el': '',       // input标签
 * 	'isMust': '',   // 是否必填
 * 	'isChange': '', // 是否实时检测
 * 	'value': ''		// 缺省值
 * }
 * @example
 * var a = new STK.module.form.formInputText(opt);
 */
$require('mod.getStrLength');
LJS.define('form.formInputText',function(ljs){
    var getStrLength = ljs.mod.getStrLength;
    var formInputText = function(opt){
        var that = this;
        if (!opt || !opt.el) 
            return false;
        // input标签
        that.el = opt.el;
        // 是否是必填项,默认不是必填(供外部重写)
        that.isMust = opt.isMust || 0;
        // 是否实时判断合法性,默认不开启
        that.isChange = opt.isChange || 0;
        // 缺省值
        that.value = opt.value || null;
        // 全局计时器
        that.timer = null;
        // 保留對父級的引用
        that.__super__ = that.prototype;
    };
    formInputText.prototype = {
        init: function(){
            var that = this;
            that.value !== null ? that.setValue(that.value) : '';
        },
        /**
         * 聚焦方法
         * @param {Object} opt 配置对象
         * opt{
         * 	callback:functon(valueLength){
         * 		// do something you want.
         * 	},
         * 	loop:function(valueLength){
         * 		// do something loop you want.
         * 		// need isChange === true
         * 	}
         * }
         */
        focus: function(opt){
            var that = this;
            $(that.el).on('focus',function(){
                opt && opt.callback ? opt.callback(that.getValueLength()) : '';
                opt && opt.loop && that.isChange ? that.change(true, opt.loop) : '';
            });
        },
        /**
         * 失焦方法
         * @param {Object} opt 配置对象
         * opt{
         * 	callback:functon(valueLength){
         * 		// do something you want.
         * 	}
         * }
         */
        blur: function(opt){
            var that = this;
             $(that.el).on('blur',function(){
                // 解除频发函数
                that.change(false);
                // 回调字数
                opt && opt.callback ? opt.callback(that.getValueLength()) : '';
                $(that).trigger('onBlur')
            });
            
        },
        /**
         * 全选方法
         * opt{
         * 	callback:functon(valueLength){
         * 		// do something you want.
         * 	},
         * 	loop:function(valueLength){
         * 		// do something loop you want.
         * 		// need isChange === true
         * 	}
         * }
         */
        select: function(opt){
            var that = this;
            $(that.el).on('select', function(){
                opt && opt.callback ? opt.callback(that.getValueLength()) : '';
                opt && opt.loop && that.isChange ? that.change(true, opt.loop) : '';
            });
        },
        /**
         * 读取input值
         * @return string 字符串
         */
        getValue: function(){
            var that = this;
            return $.trim(that.el.value);
        },
        /**
         * 写入input值
         * @param {String} value 写入的内容
         */
        setValue: function(value){
            var that = this;
            that.el.value = value;
        },
        /**
         * 读取input值长度
         * @return number 字符长度,一个汉字算两个字符
         */
        getValueLength: function(){
            var that = this;
            return getStrLength(that.getValue());
        },
        /**
         * 频发函数
         * @param {Boolean} bool true开启频发 || false关闭频发
         * @param {Object} callback 可选回调
         */
        change: function(bool, callback){
            var that = this;
            if (bool == true && callback) {
                that.timer = setInterval(function(){
                    callback(that.getValueLength());
                }, 200);
            }
            else {
                that.timer ? clearInterval(that.timer) : '';
            }
        },
        /**
         * 判断是否为空
         * @return Boolean true || false
         */
        isEmpty: function(){
            var that = this;
            return that.getValueLength() < 1;
        },
        /**
         * 置为禁用
         * @param {Boolean} bool true || false
         */
        setDisabled: function(bool){
            var that = this;
            bool ? that.el.disabled = 'disabled' : that.el.disabled = '';
        },
        /**
         * 读取input是否可读
         * @return boolean true || false
         */
        getReadOnly: function(){
            var that = this;
            return that.el.readOnly;
        },
        /**
         * 设置input是否可读
         * @param {Boolean} bool true || false
         */
        setReadOnly: function(bool){
            var that = this;
            that.el.readOnly = bool;
        },
        /**
         * 以下都为空方法,供外部重写
         */
        check: function(){
        
        },
        showTip: function(){
        
        },
        showError: function(){
        
        },
        hide: function(){
        
        }
    };
    return formInputText;
});
